package me.cxlr.shaopinghelper;

import android.app.Application;

import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.DiskLogAdapter;
import com.orhanobut.logger.Logger;

import me.cxlr.shaopinghelper.constants.Constants;
import me.cxlr.shaopinghelper.util.MmkvUtil;

/**
 * @author ShanLunZhi
 */
public class MyApplication extends Application {
    private static MyApplication application;

    @Override
    public void onCreate() {
        super.onCreate();

        application = this;

        Logger.addLogAdapter(new AndroidLogAdapter());
        Logger.addLogAdapter(new DiskLogAdapter());
        Logger.v("LogSystem init");

        // 初始化MMKV
        MmkvUtil.initMmkv();

        // 初始化数据
        this.initAppData();
    }

    /**
     * 获取全局Application
     *
     * @return application
     */
    public static MyApplication getInstance() {
        return application;
    }

    /**
     * 初始化App需要的数据
     */
    private void initAppData() {
        boolean flag = MmkvUtil.decodeBoolean("flag_first_init", true);
        if (flag) {
            // 初始化保存在MMKV中的数据
            this.initAppDataInMmkv();

            MmkvUtil.encode("flag_first_init", false);
        }
    }

    /**
     * 初始化保存在MMKV中的数据
     */
    private void initAppDataInMmkv() {
        // 主题
        MmkvUtil.encode("settings_theme", Constants.SETTING_THEME_BY_SYSTEM);
        MmkvUtil.encode("settings_brightness", 0);
        MmkvUtil.encode("settings_mode", Constants.SETTING_MODE_EBOOK);
    }
}
